<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tree_menu_ml_label.class.php');


class Tree_Menu_ML_Label_DBC extends Tree_Menu_ML_Label {
	private $table;

	function __construct($table = 'menu') {
		$this->set_table($table);
	}


	public function set_table($table) {
		$this->table = $table.'_ml_labels';
	}


	public function get_table() {
		return $this->table;
	}


	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);


		$ret = false;
		$table = addslashes($this->get_table());

		$sql = "select language, "
						." label,"
						." item"
					." from $table "
					."where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_language($rez_obj->LANGUAGE);
			$this->set_label(stripslashes($rez_obj->LABEL));
			$this->set_item($rez_obj->ITEM);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$table = addslashes($this->get_table());
		$id = $dbc->generate_id($table.'_seq');

		$language = $this->get_language();
		$label = addslashes($this->get_label());
		$item = $this->get_item();

		if ($id) {
			$sql = "insert into $table (id, "
											."language, "
											."label, "
											."item "
									      .") ".
								"values ".
									      "($id,".
									      "$language, ".
									      "'$label', ".
									      "$item ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('ID not generated - '.$table.'_seq');
		}


		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$table = addslashes($this->get_table());

		$language = $this->get_language();
		$label = addslashes($this->get_label());
		$item = $this->get_item();

		$sql = "update $table set ".
							      "language = $language, ".
							      "label = '$label', ".
							      "item = '$item' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;


		return $ret;
	}


	public function load_by_item_and_language(DB_Connection $dbc, $item_id, $language_id) {
		tangra_if_not_int_throw_e($item_id);
		tangra_if_not_int_throw_e($language_id);

		$ret = false;
		$table = addslashes($this->get_table());

		$sql = "select id from $table where item = $item_id and language = $language_id";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}


	public static function load_ml_labels_for_item(DB_Connection $dbc, $item_id, $table) {
		tangra_if_not_int_throw_e($item_id);
		$ret = array();

		$sql = "select id from ".$table."_ml_labels where item = $item_id";

		$rez = $dbc->execute($sql);
		while($rez_obj = $rez->fetch_object()) {
			$tmp = new Tree_Menu_ML_Label_DBC($table);
			$tmp->load_by_id($dbc, $rez_obj->ID);
			$ret[] = clone $tmp;
		}

		return $ret;
	}


	public static function delete_all_labels_for_item(DB_Connection $dbc, $item_id, $table) {
		tangra_if_not_int_throw_e($item_id);

		$sql = "delete from $table where item = $item_id";

		$dbc->execute($sql);
	}
}